package xFGE.xDisplay.xScene
{
	import flash.display.DisplayObjectContainer;
	
	/**
	 * 场景管理器
	 * @author Ben
	 * @version 1.0
	 * @updated 26-三月-2009 15:55:18
	 */
	public class xSceneManger
	{
		/** 场景列表 */
		private var _sceneArray:Array;
		/** 当前场景 */
		private var _currentScene:xSceneObj;
		
		/** 系统信息层 */
		private var _sysInfoLayer:xLayerObj;
		
		private static var _myInstance:xSceneManger;
		
		private var _parent:DisplayObjectContainer;
		
		private var _isInit:Boolean = false;
		
		public function xSceneManger()
		{
			if(_myInstance != null) throw("请用GetInstance获取实例");
			_sceneArray = new Array();
			_sysInfoLayer = new xLayerObj("sys_info",0,0);
		}
		/**
		 * 初始化管理器，内部创建1个命名为default的场景 
		 * @param parent
		 * 
		 */
		public function Init(parent:DisplayObjectContainer):void
		{
			if(!_isInit){
				_parent = parent;
				_isInit = true;
			}
			
			CreateScene("default",0,0);
			SwitchScene("default");
		}
		
		/**
		 * 直接创建场景 
		 * @param name
		 * @param x
		 * @param y
		 * @param w
		 * @param h
		 * 
		 */
		public function CreateScene(name:String,x:int,y:int):void
		{
			var scene:xSceneObj = new xSceneObj(name,x,y);
			AddScene(scene);
		}
		/**
		 * 添加场景 
		 * @param scene
		 * @param args
		 * 
		 */
		public function AddScene(scene:xSceneObj):void
		{
			_sceneArray.push(scene);
		}
		
		/**
		 * 获取场景对象 
		 * @param name
		 * @return 
		 * 
		 */
		public function GetSceneByName(name:String):xSceneObj
		{
			for each(var scene:xSceneObj in _sceneArray)
			{
				if(scene.name == name) return scene;
			}
			return null;
		}
		
		/**
		 * 移除场景 
		 * @param name
		 * 
		 */
		public function RemoveScene(name:String):void
		{
			var scene:xSceneObj = GetSceneByName(name);
			if(scene){
				var id:int = _sceneArray.indexOf(scene);
				_sceneArray.splice(scene,1);
			}
		}
		
		/**
		 * 切换场景 
		 * @param name
		 * @return 
		 * 
		 */
		public function SwitchScene(name:String):xSceneObj
		{
			var scene:xSceneObj = null;
			scene= GetSceneByName(name);
			if(scene == null) return null;
			else if(_parent != null){
				scene.EnterScene();
				_parent.addChild(scene);
				if(_currentScene){
					_currentScene.OutScene();
					_currentScene.removeChild(_sysInfoLayer);
					_parent.removeChild(_currentScene);
				} 
				scene.addChild(_sysInfoLayer);
				scene.SetAllwayTopMost("sys_info");
				_currentScene = scene;
				return _currentScene;
			}
			return null;
		}
		/**
		 * 获取系统信息层 
		 * @return 
		 * 
		 */
		public function get sysInfoLayer():xLayerObj
		{
			return _sysInfoLayer;
		}
		/**
		 * 获取当前场景 
		 * @return  
		 * 
		 */
		public function get currentScene():xSceneObj
		{
			return _currentScene;
		}
		
		/**
		 * 获取实例 
		 * @return 
		 * 
		 */
		public static function getInstance():xSceneManger
		{
			if(_myInstance == null) _myInstance = new xSceneManger;
			return _myInstance;
		}
		

	}
}